﻿using Microsoft.AspNetCore.Mvc;
using MiniFox.Infrastructure;
using MiniFox.Platform.Applications.Web;
using MiniFox.Utility;
using MiniFox.Web.Filters;
using MiniFox.Web.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Repository.Core.Models.Dto;
using Repository.Core.Models.Queries;
using Repository.Core.Models.VO;
using Repository.Core.Services;
using MiniFox.Platform;

namespace Repository.Core.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [NoCache, ValidationFilter]
    public class PubController : MvcControllerBase
    {
        protected IPublishService CreateComponent(string repoName)
        {
            return this.Container.GetComponent<IPublishService>(repoName, this.User);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="repoName"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<ResultObject<IEnumerable<PublishVo>>> Query([FromRoute(Name = GlobalKey.NAME)] string repoName, [FromBody] PagedQueryObject<CommonQuery> request)
        {
            var result = this.CreateComponent(repoName).Query(request);

            return result.CreateResponse();
        }

        [HttpGet]
        public Response<object> Item([FromRoute(Name = GlobalKey.NAME)] string repoName, [FromQuery(Name = GlobalKey.NAME)] string versionSetName)
        {
            object content = this.CreateComponent(repoName).GetItemContent(versionSetName);
            return content.CreateResponse();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="repoName"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Response Publish([FromRoute(Name = GlobalKey.NAME)] string repoName, [FromBody] PublishDto request)
        {
            this.CreateComponent(repoName).Publish(request);

            return new();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="repoName"></param>
        /// <param name="versionSetName"></param>
        /// <param name="versionId"></param>
        /// <returns></returns>
        [HttpGet]
        public Response Publish([FromRoute(Name = GlobalKey.NAME)] string repoName, [FromQuery(Name = GlobalKey.NAME)] string versionSetName, [FromQuery] long? versionId = null)
        {
            this.CreateComponent(repoName).Publish(versionSetName, versionId);

            return new();
        }

    }
}
